package com.rbac.web.model.req;

import com.rbac.web.model.Permission;
import com.rbac.web.model.PermissionAction;
import com.rbac.web.model.PermissionType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author gengaoliu
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "菜单添加/修改入参参数")
public class PermissionAddReq {
  @Schema(description = "id")
  private Integer id;

  @Schema(description = "创建者")
  private Long createdBy;

  @Schema(description = "路径")
  private String path;

  @Schema(description = "父Id")
  private Integer pId;

  @NotNull(message = "权限编码不可为空")
  @Schema(description = "权限编码")
  private String code;

  @NotNull(message = "权限名称不可为空")
  @Schema(description = "权限名称")
  private String name;

  @NotNull(message = "权限展示名称不可为空")
  @Schema(description = "权限展示名称")
  private String displayName;

  @Schema(description = "action")
  private String action;

  @NotNull(message = "权限类型不可为空")
  @Schema(description = "类型")
  private String type;

  @Schema(description = "icon")
  private String icon;

  @Schema(description = "value")
  private String value;

  @Schema(description = "菜单Id")
  private Integer menuId;

  @Schema(description = "排序")
  private Integer sort;

  @Schema(description = "备注")
  private String remark;

  @Schema(description = "创建时间")
  private LocalDateTime createdTime;

  @Schema(description = "多个父Id")
  private List<Integer> pIds;

  @Schema(description = "多个菜单Id")
  private List<Integer> menuIds;

  public Permission toPermission() {
    Permission permission;
    if (PermissionType.BUTTON.getValue().equals(getType())) {
      permission =
          Permission.builder()
              .id(id)
              .action(PermissionAction.getByValue(action))
              .displayName(displayName)
              .sort(sort)
              .icon(icon)
              .code(code)
              .name(name)
              .type(PermissionType.getByValue(type))
              .remark(remark)
              .path(path)
              .value(value)
              .createdTime(createdTime)
              .menuId(menuId)
              .menuIds(menuIds)
              .build();
    } else {
      permission =
          Permission.builder()
              .id(id)
              .action(PermissionAction.getByValue(action))
              .displayName(displayName)
              .value(value)
              .sort(sort)
              .icon(icon)
              .code(code)
              .name(name)
              .type(PermissionType.getByValue(type))
              .remark(remark)
              .path(path)
              .createdTime(createdTime)
              .pId(pId == null ? 0 : pId)
              .pIds(pIds)
              .build();
    }
    return permission;
  }
}
